package com.heima.common.tess4j;

import lombok.Getter;
import lombok.Setter;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.awt.image.BufferedImage;
//图片识别工具类
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "tess4j")
public class Tess4jClient {
//想要让其他微服务使用，需要在spring.factories里配置com.heima.common.tess4j.Tess4jClient
    private String dataPath;
    private String language;

    public String doOCR(BufferedImage image) throws TesseractException {
        System.out.println("开始识别图片" + image);
        //创建Tesseract对象
        ITesseract tesseract = new Tesseract();
        //设置字体库路径//设置变量，需要写在配置文件里
        tesseract.setDatapath(dataPath);
        //中文识别//设置变量，需要写在配置文件里
        tesseract.setLanguage(language);
        //执行ocr识别
        String result = tesseract.doOCR(image);
        //替换回车和tal键  使结果为一行
        result = result.replaceAll("\\r|\\n", "-").replaceAll(" ", "");
        return result;
    }

}